yjx.controller('clientInfoController', ['$rootScope', '$scope', '$fetch', '$stateParams', '$timeout', '$state', 'modalService', function ($rootScope, $scope, $fetch, $stateParams, $timeout, $state, modalService) {
    $scope.sexes = [
        { id: 1, value: '男' },
        { id: 2, value: '女' }
    ];
    $scope.checkedTags = [];
   var tags = [];

    /*解决android中软键盘弹出 将页面压缩的问题*/
    var HEIGHT = $('body').height();
    $('.m-clientInfo').height(HEIGHT)

    $scope.selectSex = function (id, value) {
        if ($scope.client) {
            $scope.client.sex = value;
        } else {
            $scope.client = {};
            $scope.client.sex = value;
        }
    }

    /**剩余字数 */
    $scope.left = function(){
        if($scope.client.remark){
            return 300 - $scope.client.remark.length;
        }else{
            return 300;
        }
    }
    $scope.showTagAct = function () {
        $scope.showTag = true;
        if ($scope.$parent.isEdit) {
            $state.go('clientDetail.editInfo.tag')
        } else {
            $state.go('clientList.info.tag')
        }

    }

    $scope.addTagAct = function (item) {
        $timeout(function () {
            $scope.checkedTags.push(item);
            for (var i = 0; i < $scope.tags.length; i++) {
                if (item.id == $scope.tags[i].id) {
                    $scope.tags.splice(i, 1);
                }
            }

        })
    }

    $scope.deleteTag = function (item) {
        $scope.tags.push(item);
        for (var i =0; i< $scope.checkedTags.length;i++) {
            if (item.id == $scope.checkedTags[i].id) {
                $scope.checkedTags.splice(i, 1);
                break
            }
        }
    }

    function spliceTag() {
        for (var i = 0; i < $scope.checkedTags.length; i++) {
            for (var j = 0; j < $scope.tags.length; j++) {
                if ($scope.checkedTags[i].id == $scope.tags[j].id) {
                    $scope.tags.splice(j, 1);
                    break;
                }
            }
        }
    }
    $scope.confirmTag = function(){
        $scope.client.tags = $scope.checkedTags;
        // console.log($scope.client.tags)
        history.back();
    }
    /*确认按钮操作*/
    $scope.editAct = function () {
        /*数据合法性验证*/
        if (!$scope.client.name) {
            modalService.mainMsgError("姓名必填")
            return
        }
        if (isNaN($scope.client.age)) {
            modalService.mainMsgError("年龄必须是数字");
            return;
        }
        if (!$scope.client.telephone) {
            modalService.mainMsgError("手机号码必填")
            return
        }
        if (!$scope.client.telephone.match("^[1][34578][0-9]{9}$")) {
            modalService.mainMsgError("请输入正确格式的手机号码")
            return
        }
        if($scope.client.tags.length>0){
            for(var i=0;i<$scope.client.tags.length;i++){
               tags.push($scope.client.tags[i].id)
            }
        }
        if ($scope.$parent.isEdit) {
            editClient();
        }
        else {
            /*希望后端可以返回id*/
            insertCleint();
        }
    }

    /*编辑用户*/
    function editClient() {
        // console.log(tags)
        $fetch.fetch(
            '/customer/edit',
            {
                params: {
                    id: $scope.client.id,
                    age: $scope.client.age,
                    name: $scope.client.name,
                    sex: $scope.client.sex,
                    telephone: $scope.client.telephone,
                    address: $scope.client.address,
                    income: $scope.client.income,
                    job: $scope.client.job,
                    remark: $scope.client.remark,
                    // cardId:$scope.client.cardId,
                    tagId:tags?tags:null,
                },
                type: 'POST',
            }
        ).then(function (data) {
            $scope.$parent.editClient.age = $scope.client.age;
            $scope.$parent.editClient.telephone = $scope.client.telephone;
            $scope.$parent.editClient.name = $scope.client.name;
            $scope.$parent.editClient.sex = $scope.client.sex;
            $scope.$parent.editClient.address = $scope.client.address;
            $scope.$parent.editClient.income = $scope.client.income;
            $scope.$parent.editClient.job = $scope.client.job;
            $scope.$parent.editClient.remark = $scope.client.remark;
            $scope.$parent.editClient.tags = $scope.client.tags;
            tags = [];
            $timeout(function () {
                history.back();
            })
        }).catch(function (e) {
            modalService.mainMsgError(e.msg);
        })
    }
    /*新增用户*/
    function insertCleint() {
        if (!$scope.client.sex) {
            $scope.client.sex = $scope.sexes[0].value;
        }
        $fetch.fetch(
            '/customer/add',
            {
                params: {
                    name: $scope.client.name,
                    age: $scope.client.age,
                    sex: $scope.client.sex,
                    telephone: $scope.client.telephone,
                    address: $scope.client.address,
                    income: $scope.client.income,
                    job: $scope.client.job,
                    remark: $scope.client.remark,
                    tagIds: tags?tags:null,
                },
                type: 'POST',
            }
        ).then(function (data) {
            $timeout(function () {
                // console.log(data);
                $scope.client.id = data;
                if ($scope.$parent.unclientList) {
                    $scope.$parent.unclientList.push($scope.client);
                } else {
                    $scope.$parent.unclientList = [$scope.client];
                }
                if ($scope.$parent.curTab.id == $scope.$parent.tabList[0].id) {
                    $scope.$parent.tabSelect($scope.$parent.tabList[1], 1);
                }
                tags = [];
                $scope.client={tags:[],sex:'男'};
                $scope.checkedTags = [];
                history.back();
            })
        }).catch(function (e) {
            modalService.mainMsgError(e.msg)
        })
    }
    /*获取tags标签列表*/

    /**获取字典数据 */
    function getDictionary() {
        var code = ['job', 'salary', 'currency'];
        $fetch.fetch(
            '/common/base/dict',
            {
                params: {
                    code: code,
                }
            }
        ).then(function (data) {
            $timeout(function () {
                $scope.job = data.job;
                $scope.salary = data.salary;
                $scope.tags = data.currency;
                if ($scope.client.tags && $scope.client.tags.length > 0) {
                    $scope.checkedTags = $scope.checkedTags.concat($scope.client.tags);
                    spliceTag();
                }else{
                    $scope.client.tags = [];
                }
                $scope.salaryData = [$scope.salary];
                $scope.industryData = [$scope.job];
                if ($scope.client.job) {
                    for (var i = 0; i < data.job.length; i++) {
                        if ($scope.client.job == data.job[i].value) {
                            $scope.industryIndex = [i]
                        }
                    }
                } else {
                    $scope.industryIndex = [Math.floor($scope.job.length / 2)];
                }

                if ($scope.client.income) {
                    for (var i = 0; i < data.salary.length; i++) {
                        if ($scope.client.income == data.salary[i].value) {
                            $scope.salaryIndex = [i];
                        }
                    }
                } else {
                    $scope.salaryIndex = [Math.floor($scope.salary.length / 2)];
                }
            })
        }).catch(function (e) {
            modalService.mainMsgError(e.msg);
        })
    }
    if ($scope.$parent.editClient) {
        $scope.client = $scope.$parent.editClient;
        getDictionary();
    }

    /*字典数据*/
    // 行业
    $scope.industrySelect = function (value, id) {
        $timeout(function () {
            $scope.client.job = $scope.job[id].value
        })
    }
    $scope.industryChange = function (dataIndex, index) {
    }
    //薪水 
    $scope.salarySelect = function (value, id) {
        $timeout(function () {
            $scope.client.income = $scope.salary[id].value;
        })
    }
    $scope.salaryChange = function (dataIndex, index) {
    }
}])